#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
using namespace std;
#define rep(Ii,Jj,Kk) for(int Ii=(Jj),Ii##_=(Kk);Ii<=Ii##_;Ii++)
#define per(Ii,Jj,Kk) for(int Ii=(Jj),Ii##_=(Kk);Ii>=Ii##_;Ii--)
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned uint;
typedef long double db;
#define fir first
#define sec second
#define siz(Aa) ((int)(Aa).size())
#define all(Aa) (Aa).begin(),(Aa).end()
#define ckmx(Aa,Bb) (Aa=max(Aa,Bb))
#define ckmn(Aa,Bb) (Aa=min(Aa,Bb))

const int N=2e5+10;
int n,m,a[N],ans[N],root,al,sz[N],w[1<<15];
vector<int> e[N];
vector<pair<int,int>> q[N];
bool vis[N];
void grt(int rt,int fa){
	sz[rt]=1;
	int mx=0;
	for(int i:e[rt]) if(i!=fa && !vis[i]){
		grt(i,rt);
		sz[rt]+=sz[i];
		ckmx(mx,sz[i]);
	}
	if(max(mx,al-sz[rt])*2<=al) root=rt;
}
void ins(int val,int coe){// n log
	int s=(((1<<15)-1)^val)>>9<<9;
	for(int i=s;;i=(i-1)&s){
		w[val|i]+=coe;
		if(!i) break;
	}
}
int que(int val){// n
	int s=val&((1<<9)-1);
	val=val>>9<<9;
	int res=0;
	for(int i=s;;i=(i-1)&s){
		res+=w[val|i];
		if(!i) break;
	}
	return res;
}
void chk(int rt,int fa,int val,int coe,int anc){
	val|=a[rt];
	ins(val,coe);
	if(anc && rt!=anc){
		vector<pair<int,int>> tmp;
		for(auto[ned,id]:q[rt]){
			((ned|val)==ned?q[anc]:tmp).emplace_back(ned,id);
		}
		q[rt]=tmp;
	}
	for(int i:e[rt]) if(i!=fa && !vis[i]){
		chk(i,rt,val,coe,anc);
	}
}
void slv(int rt){
	chk(rt,0,0,1,rt);
	for(auto[ned,id]:q[rt]){
		ans[id]=que(ned);
	}
	chk(rt,0,0,-1,0);
	grt(rt,0);
	vis[rt]=true;
	for(int i:e[rt]) if(!vis[i]){
		al=sz[i];
		grt(i,0);
		slv(root);
	}
}
signed main(){ios::sync_with_stdio(false),cin.tie(nullptr);
	freopen("tree.in","r",stdin);
	freopen("tree.out","w",stdout);
	int orz;
	cin>>n>>m>>orz;
	rep(i,1,n){
		int x;cin>>x;
		a[i]=1<<x;
	}
	rep(i,2,n){
		int x;cin>>x;
		e[x].emplace_back(i);
		e[i].emplace_back(x);
	}
	rep(i,1,m){
		int x,y;cin>>x>>y;
		q[y].emplace_back(x,i);
	}
	al=n;
	grt(1,0);
	slv(root);
	rep(i,1,m) cout<<max(1,ans[i])<<"\n";
return 0;}
/*
*/